package cn.zl.ZLM.event.callback.impl;

import cn.hutool.core.util.StrUtil;
import cn.zl.ZLM.core.ZLMApi;
import cn.zl.ZLM.event.callback.inter.IMKPlayCallBack;
import cn.zl.ZLM.helper.ZLMApiHelper;
import cn.zl.ZLM.structure.MK_AUTH_INVOKER;
import cn.zl.ZLM.structure.MK_MEDIA_INFO;
import cn.zl.ZLM.structure.MK_SOCK_INFO;
import com.sun.jna.CallbackThreadInitializer;
import com.sun.jna.Native;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class MKPlayCallBack implements IMKPlayCallBack {

    private static final Logger logger = LoggerFactory.getLogger(MKPlayCallBack.class);

    public MKPlayCallBack() {
        Native.setCallbackThreadInitializer(this, new CallbackThreadInitializer(true, false, "MKPlayThread"));
    }

    @Override
    public void invoke(MK_MEDIA_INFO url_info, MK_AUTH_INVOKER invoker, MK_SOCK_INFO sender) {
        ZLMApi ZLM_API = ZLMApiHelper.getInstance().getApi();
        //通过ZLM_API.mk_media_info_get_*可以查询流的各种信息
        String schema = ZLM_API.mk_media_info_get_schema(url_info);
        String host = ZLM_API.mk_media_info_get_host(url_info);
        int port = ZLM_API.mk_media_info_get_port(url_info);
        String app = ZLM_API.mk_media_info_get_app(url_info);
        String stream = ZLM_API.mk_media_info_get_stream(url_info);
        //这里拿到访问路径后(例如rtmp://xxxx/xxx/xxx?token=xxxx其中?后面就是拿到的参数)的参数
        String params = ZLM_API.mk_media_info_get_params(url_info);
        // err_msg返回 空字符串表示鉴权成功 否则鉴权失败提示
        ZLM_API.mk_auth_invoker_do(invoker, "");
        //MediaServerContext.ZLM_API.mk_auth_invoker_do(invoker, "播放鉴权失败");
        if(StrUtil.isBlank(params)){
            logger.info("[正在播放] {}://{}:{}/{}/{}", schema, host, port, app, stream);
        } else {
            logger.info("[正在播放] {}://{}:{}/{}/{}?{}", schema, host, port, app, stream, params);
        }
    }
}
